﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using YuuzleComic.DBUtility;
using System.Data;
using System.Data.SqlClient;
using NPinyin;
using Maticsoft.Common;
using YuuzleComic.Common;

public partial class search : YuuzleComic.Web.UI.BasePage
{
    protected string skey = "";
    protected int pageSize = 15;
    protected DataTable dt;
    protected int pageIndex = 1;
    protected int pageCount = 0;
    protected int recordCount = 0;
    protected string url = "";
    protected string linkcls = "";
    protected string linkcls1 = "";
    protected string linkcls2 = "";
    protected YuuzleComic.BLL.ad adbll = new YuuzleComic.BLL.ad();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            skey = Input.FilterHTML(Request["key"]).Trim();
            SqlParameter[] parameters = new SqlParameter[]{
                    new SqlParameter("@chkey",SqlDbType.VarChar),
                    new SqlParameter("@enkey", SqlDbType.VarChar)
                };
            parameters[0].Value = "\"" + skey + "\"";
            parameters[1].Value = "\"" + Pinyin.GetPinyin(skey).Replace(" ", "") + "*\"";
            if (Request.Form.Count > 0)
	        {
                DataTable dt = DbHelperSQL.Query("select top(" + pageSize + ") s.mc1 mhmc,a.id,a.mhbm,a.zt,a.hits,a.mhfm,a.tj,a.adddate cdate,left(a.mhjj,45)+'……' as mhjs,c.id zjid,c.zjmc,c.time,STUFF((select ',' +cast(zzid as varchar) from comics_zz where cid=a.id FOR XML PATH('')), 1, 1, '') zzid,STUFF((SELECT ','+ z.zzmc FROM zz z join comics_zz cz on z.id=cz.zzid where cz.cid=a.id FOR XML PATH('')), 1, 1, '') zzmc from search s join comics a on s.cid=a.id join (select MAX(ordernum) ordernum,cid from zj group by cid) b on a.id=b.cid join zj c on b.ordernum=c.ordernum where contains((s.mc1,s.mc2,s.mc5),@chkey) or contains((s.mc3,s.mc4),@enkey) order by hits desc", parameters).Tables[0];
                JArray ja = new JArray();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i >= pageSize)
                    {
                        break;
                    }
                    DataRow dr = dt.Rows[i];
                    JObject jo = new JObject();
                    jo.Add("id", Convert.ToInt32(dr["id"]));
                    if (Request.Form["s"] == "1")
                    {
                        jo.Add("title", ("<p>" + dr["mhmc"].ToString() + "</p>&nbsp;<acronym>[" + dr["zzmc"] + "]</acronym>").Replace(skey, "<strong>" + skey + "</strong>"));
                        jo.Add("status", Convert.ToBoolean(dr["zt"]) ? "<em>已完结</em>" : "<span>" + dr["zjmc"] + "</span>");
                    }
                    else
                    {
                        jo.Add("title", dr["mhmc"].ToString());
                        jo.Add("status", Convert.ToBoolean(dr["zt"]) ? "<em>已完结</em>" : "<span>" + dr["zjmc"] + "</span>");
                        jo.Add("author", dr["zzmc"].ToString());
                    }
                    ja.Add(jo);
                }
                Response.Write(ja.ToString());
                Response.End();
	        }
            else if (Request.QueryString.Count > 0)
            {
                if (Request.QueryString["p"] != null)
                {
                    pageIndex = Convert.ToInt32(Request.QueryString["p"]);
                    pageIndex = (pageIndex < 1 ? 1 : pageIndex);
                }
                string strOrder = "time";
                switch (Request.QueryString["o"])
                {
                    case "1":
                        url = "search.aspx?key=" + skey + "&o=1";
                        strOrder = "cdate";
                        linkcls1 = "class=on";
                        break;
                    case "2":
                        url = "search.aspx?key=" + skey + "&o=2";
                        strOrder = "hits";
                        linkcls2 = "class=on";
                        break;
                    default:
                        url = "search.aspx?key=" + skey;
                        linkcls = "class=on";
                        break;
                }
                dt = DbHelperSQL.Query("SELECT TOP " + pageSize + " * FROM (select ROW_NUMBER() OVER (ORDER BY t." + strOrder + " desc) AS RowNumber,* from (select s.mc1 mhmc,a.id,a.mhbm,a.zt,a.hits,a.mhfm,a.tj,a.adddate cdate,left(a.mhjj,45)+'……' as mhjs,c.id zjid,c.zjmc,c.time,STUFF((select ',' +cast(zzid as varchar) from comics_zz where cid=a.id FOR XML PATH('')), 1, 1, '') zzid,STUFF((SELECT ','+ z.zzmc FROM zz z join comics_zz cz on z.id=cz.zzid where cz.cid=a.id FOR XML PATH('')), 1, 1, '') zzmc from search s join comics a on s.cid=a.id join (select MAX(ordernum) ordernum,cid from zj group by cid) b on a.id=b.cid join zj c on b.ordernum=c.ordernum where contains((s.mc1,s.mc2,s.mc5),@chkey) or contains((s.mc3,s.mc4),@enkey)) t) P WHERE RowNumber > " + pageSize * (pageIndex - 1), parameters).Tables[0];
                recordCount = Convert.ToInt32(DbHelperSQL.Query("select count(s.id) from search s join comics a on s.cid=a.id join (select MAX(ordernum) ordernum,cid from zj group by cid) b on a.id=b.cid join zj c on b.ordernum=c.ordernum join zz d on a.zzid=d.id where contains((s.mc1,s.mc2,s.mc5),@chkey) or contains((s.mc3,s.mc4),@enkey)", parameters).Tables[0].Rows[0][0]);
                pageCount = (recordCount + pageSize - 1) / pageSize;

                if (recordCount == 0)
                {
                    SqlParameter[] paramters = new SqlParameter[] { new SqlParameter("@keyword", skey) };
                    int row = DbHelperSQL.ExecuteSql("update search_no set count=count+1,ip='" + Request.UserHostAddress + "' where id=(select top(1) id from search_no where keyword=@keyword)", paramters); ;
                    if (row <= 1)
                    {
                        DbHelperSQL.ExecuteSql("INSERT INTO search_no (keyword,count,ip) VALUES (@keyword,1,'" + Request.UserHostAddress + "')", new SqlParameter[] { new SqlParameter("@keyword", skey) });
                    }
                }
            }
        }
    }

    protected string createFoot() {
        string s = YuuzleComic.Common.Input.ReadTextFile("~/admin/temp/common/foot.html");
        s = s.Replace("[WEB_URL]", web_url).Replace("[WEB_NAME]", web_name);
        return s;
    }

    protected string createTop_nav()
    {
        string s = YuuzleComic.Common.Input.ReadTextFile("~/admin/temp/common/top-nav.html");
        s = s.Replace("[WEB_URL]", web_url).Replace("[WEB_NAME]", web_name);
        return s;
    }
    protected string createTop_main()
    {
        string s = YuuzleComic.Common.Input.ReadTextFile("~/admin/temp/common/top-main.html");
        s = s.Replace("[WEB_URL]", web_url).Replace("[WEB_NAME]", web_name);
        return s;
    }
}